﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Diagnostics;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using FaceSharp.Api;

namespace FaceSharp.MVC.Areas.FacebookApp.Controllers
{
    public class BaseController : AsyncController
    {
        private readonly Stopwatch _stopwatch = new Stopwatch();

        public static string ApplicationId;
        public static string ApplicationSecret;
        public static string ApplicationUrl;
        public static string RedirectUrl;
        public static string ExtendedPermissions;

        public BaseController()
        {
            _stopwatch.Start();
        }

        protected override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            filterContext.HttpContext.Response.AddHeader("P3P", "CP=\"CAO PSA OUR\"");
            
            var appToken = new FacebookApi().Application.GetApplicationAccessToken();
            ViewData["ApplicationToken"] = appToken;

            ViewData["ApplicationId"] = ConfigurationManager.AppSettings["ApplicationId"];
            ViewData["ApplicationUrl"] = ConfigurationManager.AppSettings["ApplicationUrl"];
            ViewData["SiteRootUrl"] = ConfigurationManager.AppSettings["SiteRootUrl"];
            ViewData["ExtendedPermissions"] = ConfigurationManager.AppSettings["ExtendedPermissions"];

            // Optional: Work only for GET request and Do not work with AjaxRequests
            if (!filterContext.RequestContext.HttpContext.Request.IsAjaxRequest())
            {
                string areaName = "";
                if (filterContext.RouteData.DataTokens.ContainsKey("area"))
                {
                    areaName = ((string)filterContext.RouteData.DataTokens["area"]);
                    if (areaName != null)
                    {
                        areaName = areaName + "-";
                    }
                    else
                    {
                        areaName = "";
                    }
                }

                ViewData["BodyId"] = (areaName + filterContext.RouteData.Values["controller"].ToString() + "-" + filterContext.RouteData.Values["action"].ToString()).ToLower();
            }

            base.OnActionExecuting(filterContext);
        }

        protected override void OnException(ExceptionContext filterContext)
        {
            // Bail if we can't do anything; app will crash.
            if (filterContext == null)
                return;

            var ex = filterContext.Exception ?? new Exception("No further information exists.");
            Trace.Write("BaseController.Exception :" + ex.Message + ex.Source + ex.StackTrace);

            filterContext.ExceptionHandled = true;
            filterContext.Result = View("ErrorPage");
        }

        protected override void OnActionExecuted(ActionExecutedContext filterContext)
        {
            _stopwatch.Stop();
            ViewData["ExecutionTime"] = _stopwatch.ElapsedMilliseconds;
            base.OnActionExecuted(filterContext);
        }
    }
}
